import { SignJWT, importPKCS8 } from 'jose';

// PKCS#8格式的私钥需要包含BEGIN和END标记
const YourPrivateKey = `-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEIFHnYRlbIh9ScuYYcP/TVhwjJqjE0eZIMSsESAoMXOPe
-----END PRIVATE KEY-----`

importPKCS8(YourPrivateKey, 'EdDSA').then((privateKey) => {
  const customHeader = {
    alg: 'EdDSA',
    kid: 'TBB6BPY6HK'
  }
  const iat = Math.floor(Date.now() / 1000) - 30;
  const exp = iat + 60*60*24;
  const customPayload = {
    sub: '2D88EU8PER',
    iat: iat,
    exp: exp
  }
  new SignJWT(customPayload)
    .setProtectedHeader(customHeader)
    .sign(privateKey)
    .then(token => console.log('JWT: ' + token))
}).catch((error) => console.error(error))